from main import basedir

import platform
from flask import request, make_response, send_file, session
import time, os
from main.code import *
from main.utils import InterfaceHelper, RequestHelper, FileHelper, snowflake, ImageHelper
from main.dbs.equipment import *
from main.dbs.common import *
from flask_restplus import Api, Resource, fields
from . import api, check_cookie, set_session, check_admin_cookie

# 定义命名空间
ns = api.namespace('equipment', description='Equipment api')

equipment_model = api.model('EquipmentModel', {
    'id': fields.Integer(readOnly=True, description='用户id'),
    'image': fields.String(),
    'status': fields.Integer(description='状态'),
    'borrow_status': fields.Boolean(description='状态'),
    'name': fields.String()
    # 'create_time': fields.DateTime(description='时间')
})

equipmentBorrow_model = api.model('EquipmentBorrowModel', {
    'id': fields.Integer(readOnly=True, description='用户id'),
    'equipment_id': fields.Integer(description='用户id'),
    'subscribe_id': fields.Integer(description='用户id'),
    'start_time': fields.Date(),
    'status': fields.Integer(description='状态'),
    # 'create_time': fields.DateTime(description='时间')
})


@ns.route('')
class EquipmentApi(Resource):

    @ns.expect(equipment_model)
    def post(self):
        '''添加设备'''
        user_id = check_cookie()
        parameters = RequestHelper.formToDict(request)
        equ = JSONHelper.dictToObj(parameters, Equipment)
        if insert(equ):
            return success()
        return fail()


@ns.route('/list')
class EquipmentListApi(Resource):

    @ns.param('')
    def get(self):
        '''获得列表'''
        user_id = check_cookie()
        parameters = RequestHelper.formToDict(request)
        data = []
        for i in query_equipment_list():
            data.append(i.to_json())
        return success(data)


@ns.route('/borrow')
class EquipmentBorrowApi(Resource):

    @ns.expect(equipment_model)
    def post(self):
        '''借用设备'''
        user_id = check_cookie()
        parameters = RequestHelper.formToDict(request)
        equipmentUsed = JSONHelper.dictToObj(parameters, EquipmentUsed)
        equipmentUsed.subscribe_id = user_id
        if insert(equipmentUsed):
            equip = query_equipment_by_id(parameters['equipment_id'])
            equip.borrow_status = True
            update(equip)
            return success()
        return fail()


@ns.route('/back')
class EquipmentBackApi(Resource):

    @ns.param('equipment_id')
    def post(self):
        '''归还设备'''
        user_id = check_cookie()
        parameters = RequestHelper.formToDict(request)
        eu = query_equipmentUsed_by_id(parameters['id'])
        equipment_id = eu.equipment_id
        # if eu.examine_status == 2:
        #     return fail('设备已归还')
        eu.examine_status = 2
        if update(eu):
            e = query_equipment_by_id(equipment_id)
            e.borrow_status = False
            update(e)
            return success()
        return fail()


@ns.route('/used/list')
class EquipmentUsedListApi(Resource):

    @ns.param('subscribe_id')
    def get(self):
        '''获得列表'''
        user_id = check_cookie()
        parameters = RequestHelper.formToDict(request)
        data = []
        for i in query_equipmentUsed_list(parameters):
            i.equipment = query_equipment_by_id(i.equipment_id).to_json()
            data.append(i.to_json())
        return success(data)
